<!DOCTYPE>
<html>
<head>
    <meta charset="utf-8">
    <title>IFE JavaScript Task 01</title>
</head>
<body>
<ul id="source">
    <li>北京空气质量：<b>90</b></li>
    <li>上海空气质量：<b>70</b></li>
    <li>天津空气质量：<b>80</b></li>
    <li>广州空气质量：<b>50</b></li>
    <li>深圳空气质量：<b>40</b></li>
    <li>福州空气质量：<b>32</b></li>
    <li>成都空气质量：<b>90</b></li>
</ul>
<ul id="resort">
    <!--
    <li>第一名：北京空气质量：<b>90</b></li>
    <li>第二名：北京空气质量：<b>90</b></li>
    <li>第三名：北京空气质量：<b>90</b></li>
     -->
</ul>
<button id="sort-btn">排序</button>
<script type="text/javascript">
    function getData(){
        var data=[];
        var ul=document.getElementById("source");
        for(var i=0;i<ul.childElementCount;i++){
            var li  = ul.children[i];
            var cityName = li.innerHTML.split("空气质量：")[0];
            var aqi = Number(li.children[0].innerHTML);
            data.push([cityName,aqi]);
        }
        return data;
    }
    function sortAqiData(data) {
        data.sort(function(a,b){
           return a[1]-b[1];
        });
        return data;
    }
    function render(data) {
        var chineseNumbers = ["一","二","三","四","五","六","七","八","九","十"];
        var content="";
        data.forEach(function(element,index){
            content+="<li>第"+chineseNumbers[index]+"名："+element[0]+"空气质量：<b>"+element[1]+"</b></li>";
        });
        document.getElementById("resort").innerHTML=content;
    }
    function btnHandle() {
        var aqiData = getData();
        aqiData = sortAqiData(aqiData);
        render(aqiData);
    }
    function init() {
        var btn=document.getElementById("sort-btn");
        btn.onclick=function(){
            btnHandle();
        };
    }
    init();
</script>
</body>
</html>